package com.dotflat.rest.utils;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;


public class HttpHelperUtil {

        public HttpHelperUtil() {
        }

        public static String get(String url) {
            try {
                CloseableHttpClient httpclient = HttpClients.createDefault();
                HttpGet httpget = new HttpGet(url);
                CloseableHttpResponse response = httpclient.execute(httpget);

                String var5;
                try {
                    if (response.getStatusLine().getStatusCode() != 200) {
                        System.out.println("Http " + response.getStatusLine().getStatusCode() + " error");
                        return null;
                    }

                    String strResult = EntityUtils.toString(response.getEntity());
                    var5 = strResult;
                } finally {
                    response.close();
                }

                return var5;
            } catch (IOException var10) {
                var10.printStackTrace();
                return null;
            }
        }

        public static Map getMap(String url) {
            String result = get(url);
            if (result == null) {
                return null;
            } else {
                Map map = JsonHelper.toMap(result);
                return map;
            }
        }


        public static String post(String url, Object data){
            return post(url, data,"text/plain;charset=UTF-8");
        }

        public static String post(String url, Object data, String contentType) {
            try {
                CloseableHttpClient httpclient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setHeader("Content-Type", contentType);
                String strData;
                if ("application/json".equalsIgnoreCase(contentType)) {
                    strData = JsonHelper.toStr(data);
                } else {
                    strData = data.toString();
                }

                StringEntity requestEntity = new StringEntity(strData, contentType, "utf-8");
                httpPost.setEntity(requestEntity);
                CloseableHttpResponse response = httpclient.execute(httpPost);

                String var9;
                try {
                    if (response.getStatusLine().getStatusCode() != 200) {
                        //System.out.println("Http " + response.getStatusLine().getStatusCode() + " error");
                        return null;
                    }

                    String strResult = EntityUtils.toString(response.getEntity());
                    var9 = strResult;
                } finally {
                    response.close();
                }

                return var9;
            } catch (IOException var14) {
                var14.printStackTrace();
                return null;
            }
        }

        public static Map postMap(String url, Object data) {
            String result = post(url, data, "application/json");
            if (result == null) {
                return null;
            } else {
                Map map = JsonHelper.toMap(result);
                return map;
            }
        }

        public static String postForm(String url, Map data) {
            try {
                CloseableHttpClient httpclient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
                Set<String> keys = data.keySet();
                List<NameValuePair> formparams = new ArrayList();
                Iterator var6 = keys.iterator();

                while(var6.hasNext()) {
                    String key = (String)var6.next();
                    formparams.add(new BasicNameValuePair(key, data.get(key)==null?"":data.get(key).toString()));
                }

                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
                httpPost.setEntity(entity);
                CloseableHttpResponse response = httpclient.execute(httpPost);

                String var9;
                try {
                    if (response.getStatusLine().getStatusCode() != 200) {
                        System.out.println("Http post form " + response.getStatusLine().getStatusCode() + " error");
                        return null;
                    }

                    String strResult = EntityUtils.toString(response.getEntity());
                    var9 = strResult;
                } finally {
                    response.close();
                }

                return var9;
            } catch (IOException var14) {
                var14.printStackTrace();
                return null;
            }
        }


}
